<?php

/**
 * gmaPHP - A rudimental PHP abstraction layer for the Google Maps API v3
 *
 * @author      Nils Abegg
 * @version     die Pre-Beta des RC2 der Alpha Version
 * @package     Google
 * @subpackage  Maps
 * @category    Google API Abstraction Layer
 * @filesource
 * 
 * @todo Batch Geoencoder
 * @todo Icon Support
 */
class gmaPHP4 
{
    
    
    var $_generateJsApi = true;
    
    var $_generateJsTags = true;
    
    /**
     * json
     * 
     * @access protected
     * @var Services_JSON
     */
    var $_json = null;
    
    /**
     * mapElement
     * 
     * @access protected
     * @var string 
     */
    var $_mapElement = 'map-canvas';
    
    /**
     * markers
     *     
     * @access protected
     * @var mixed 
     */
    var $_markers = array();
    
    /**
     * options
     *     
     * @access protected
     * @var mixed 
     */
    var $_options = array(
        'zoom' => '5', 

        'latitude' => '49.00657',
        'longitude' => '8.50320',
        'maptype' => 'ROADMAP'
    );
    
    var $_sensor = 'false';
    
    /**
     * constructor
     * 
     * The constructor sets the Services_JSON object if you
     * use PHP4, or if you don't wan't to use the json_encode() 
     * or json_decode() functions of PHP5.
     * 
     * @param type $json 
     */
    function gmaPHP4($json = null)
    {
        
        $this->_json = $json;
        
    }
    
    /**
     * addMarker
     * 
     * This method adds a marker to the map. 
     * If you don't provide the $content parameter, there will be 
     * no info window generated.
     * 
     * @access public
     * @param string $latitude
     * @param string $longitude
     * @param string $title
     * @param string $content 
     */
    function addMarker($latitude, $longitude, $title = null, $content = null)
    {
        
        $this->_markers[] = array(
            'latitude' => $latitude,
            'longitude' => $longitude,
            'title' => $title,
            'content' => $content    
        );
       
    }
    
    /**
     * geocode
     * 
     * This method returns the geo coordinates for an location.
     * It requests JSON output from the Google API for 
     * easier response parsing.
     * 
     * @access public
     * @param  string $location
     * @return mixed 
     */
    function geocode($location)
    {
        
        $requestUrl = 'http://maps.google.com/maps/api/geocode/json?address=' . urlencode($location) . '&sensor=false';
        $jsonResponse = file_get_contents($requestUrl) or die('url not loading');
        $json = $this->_json->decode($jsonResponse);
        $response['status'] = $json->status;
        $response['latitude'] = $json->results[0]->geometry->location->lat;
        $response['longitude'] = $json->results[0]->geometry->location->lng;

        return $response;
        
    }
    
    /**
     * getJavascript
     * 
     * This method generates the JavaScript for the map.
     * 
     * @access public
     * @return string 
     */
    function getJavascript()
    {
        
        $javascript = '';
        if ($this->_generateJsApi == true)
        {
            $javascript .= $this->getJavascriptApi();
        }
        if($this->_generateJsTags == true)
        {
            $javascript .= '<script type="text/javascript">';
        }
        $javascript .= 'function gmapInitialize() {';
        $javascript .= "\n";
        $javascript .= 'var centerPosition = new google.maps.LatLng(' . $this->_options['latitude'] . ', ' . $this->_options['longitude'] . ');';
        $javascript .= "\n";
        $javascript .= 'var gmapOptions = {';
        $javascript .= "\n";
        $javascript .= 'zoom: ' . $this->_options['zoom'] . ',';
        $javascript .= "\n";
        $javascript .= 'center: centerPosition,';
        $javascript .= "\n";
        $javascript .= 'mapTypeId: google.maps.MapTypeId.' . $this->_options['maptype'];
        $javascript .= "\n";
        $javascript .= '};';
        $javascript .= "\n";
        $javascript .= 'var gmap = new google.maps.Map(document.getElementById("' . $this->_mapElement . '"), gmapOptions);';
        if(isset($this->_markers[0]) == true)
        {
            $javascript .= $this->_getMarkerJavascript();
        } 
        $javascript .= "\n";
        $javascript .= '}';
        if($this->_generateJsTags == true)
        {
            $javascript .= '</script>';
        }
        
        return $javascript;
        
    }
    
    /**
     *
     * @access public
     * @return string 
     */
    function getJavascriptApi()
    {
        
        $javascript = '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=' . $this->_sensor . '"></script>';
    
        return $javascript;
        
    }
    
    /**
     * setCenterPosition
     * 
     * This method sets the center point of the map.
     * 
     * @param string $latitude
     * @param string $longitude
     * @param integer $radius 
     */
    function setCenterPosition($latitude, $longitude, $radius)
    {
        
        $this->_options['latitude'] = $latitude;
        $this->_options['longitude'] = $longitude;
        if($radius < 10)
        {
            $this->_options['zoom'] = '12';
        }
        elseif($radius <= 15)
        {
            $this->_options['zoom'] = '11';
        }
        elseif($radius == 25)
        {
            $this->_options['zoom'] = '10';
        }
        elseif($radius <= 40)
        {
            $this->_options['zoom'] = '9';
        }
        elseif($radius == 50)
        {
            $this->_options['zoom'] = '8';
        }
        elseif($radius == 300)
        {
            $this->_options['zoom'] = '5';
        }
        
    }
    
    /**
     * setMapElement
     * 
     * This method sets the HTML element which contains the map.
     * 
     * @access public
     * @param type $mapElement 
     */
    function setMapElement($mapElement)
    {
        
        $this->_mapElement = $mapElement;
        
    }
    
    /**
     * setOption
     * 
     * This Method sets an option for the map.
     * 
     * @access public
     * @param string $option
     * @param string $value 
     */
    function setOption($option, $value)
    {
        
        $this->_options[$option] = $value;
        
    }
    
    function setSensor($sensor)
    {
        
        $this->_sensor = $sensor;
        
    }
    
    /**
     * getMarkerJavascript
     * 
     * This method generates the JavaScript for the markers 
     * of the map.
     * 
     * @access protected
     * @return string 
     */
    function _getMarkerJavascript()
    {
        
        $i = 1;
        $javascript = '';
        foreach($this->_markers as $marker)
        {  
            $javascript .= 'var marker' . $i . ' = new google.maps.Marker({';
            $javascript .= 'position: new google.maps.LatLng(' . $marker['latitude'] . ', ' . $marker['longitude'] . '),';
            $javascript .= 'map: gmap,'; 
            $javascript .= 'title:"' . $marker['title'] . '"';
            $javascript .= '});';
            if($marker['content'] != null)
            {
                $javascript .= 'var infowindow' . $i . ' = new google.maps.InfoWindow({';
                $javascript .= 'content: "' . $marker['content'] . '"';
                $javascript .= '});';
                $javascript .= 'google.maps.event.addListener(marker' . $i . ', \'click\', function() {';
                $javascript .= 'infowindow' . $i . '.open(gmap,marker' . $i . ');';
                $javascript .= '});';
            }
            $i++;
        }

        return $javascript;
        
    }
    
}

?>

